#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
计划
script name: plan
Created on 2020/3/10 13:26 by PyCharm
@author: fujianjian
"""
from datetime import datetime

from app import db


class Plan(db.Model):

    __tablename__= 't_plans'
    id = db.Column(db.Integer, primary_key=True, comment='主键')
    name = db.Column(db.String(127), nullable=False, comment='名称')
    create_time = db.Column(db.TIMESTAMP, default=datetime.utcnow(), comment='创建时间')
    update_time = db.Column(db.TIMESTAMP, default=datetime.utcnow(), comment='更新时间')

    def keys(self):
        return ['id', 'name', 'create_time', 'update_time']

    def __getitem__(self, item):
        attr = getattr(self, item)
        if isinstance(attr, datetime):
            attr = attr.strftime('%Y-%m-%d %H:%M:%S')

        return attr


class PlanItem(db.Model):

    __tablename__='t_plan_items'

    id = db.Column(db.Integer, primary_key=True, comment='主键')
    seq = db.Column(db.Integer, nullable=False, comment='序号')
    timeline = db.Column(db.String(127), nullable=False, comment='时间点')
    plan_desc = db.Column(db.String(512), comment='描述')
    plan_id = db.Column(db.Integer, nullable=False, comment='plan_id')

    def keys(self):
        return ['id', 'seq', 'timeline', 'plan_desc']

    def __getitem__(self, item):
        attr = getattr(self, item)
        if isinstance(attr, datetime):
            attr = attr.strftime('%Y-%m-%d %H:%M:%S')
        return attr

